﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Micrhard.FMCG.Server.Models;
using Microhard.Permission.Interface;
using Micrhard.FMCG.Server.Interface;
using Microsoft.Practices.Unity;
using Telerik.Web.Mvc;
using Microhard.Core.DBUtility;
using Microhard.Protal.Interfaces;
using Microhard.Core;
using FluentValidation.Mvc;
using Microhard.Permission.MVCFilter;
using Microhard.Permission.MVCFilter.MVCFilter;

namespace Microhard.FMCG.WebUI.Areas.Orders.Controllers
{
    public class SaleOrderController : MicroHardBaseController<OrderModel>
    {
        private ICustomerOrder _resp = null;
        [Dependency]
        public ICustomerOrder Isl
        {
            set
            {
                base._repository = value;
                _resp = value;
            }
            get
            {
                return _resp;
            }
        }

        private void LoadPrdDataInViewData(ScopeResult scope)
        {
            ICmpProduct prd = IoC.Resolve<ICmpProduct>();
            var prds = prd.GetPrdsByCompanyID(scope.EmpInfo.CompanyID);
            ViewData["prds"] = prds.Select(p => new SelectListItem
            {
                Text = p.Mark,
                Value = p.SN
            }).ToList();
        }

        public override ActionResult Create(Guid? newPKID, ScopeResult scope)
        {
            this.LoadPrdDataInViewData(scope);
            return base.Create(newPKID, scope);
        }

        public override ActionResult Update(Guid identity, ScopeResult scope)
        {
            this.LoadPrdDataInViewData(scope);
            return base.Update(identity, scope);
        }

        public override ActionResult SaveUpdate([CustomizeValidator(RuleSet = "Update")]OrderModel model, ScopeResult scope)
        {
            this.LoadPrdDataInViewData(scope);
            return base.SaveUpdate(model, scope);
        }

        [MVCFilterAtrForLogon(Order = 0)]
        [MVCFilterAtrForResourcePermission(OperationKeyName = "Update", Order = 1)]
        [MVCFilterAtrForDataPermission(OnExecutingFilterDataType = "Update", Order = 2)]
        public ActionResult CloseOrder(Guid identity, ScopeResult scope)
        {
            _resp.scope = scope;
            _resp.CloseOrder(identity);
            return RedirectToAction("Index");
        }

        [MVCFilterAtrForWeixinMP(Order = 0)]
        [MVCFilterAtrForLogon(Order = 1)]//登录验证
        public ActionResult AddInShopCart(string sku, int orderQty)
        {
            List<OrderDetailModelInServer> sc = TempData["shopcart"] as List<OrderDetailModelInServer>;
            if (sc == null)
            {
                sc = new List<OrderDetailModelInServer>(); ;
            }
            sc.Add(new OrderDetailModelInServer
            {
                SKU = sku,
                OrderQty = orderQty
            });
            TempData["shopcart"] = sc;
            var rtn = new JsonResult();
            rtn.Data = true;
            rtn.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return rtn;
        }
    }
}
